package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.common.vo.LoginUserInfoVo;
import com.yike.user.entity.Machine;
import com.yike.user.entity.Shop;
import com.yike.user.entity.ShopCamera;
import com.yike.user.mapper.MachineMapper;
import com.yike.user.mapper.ShopCameraMapper;
import com.yike.user.mapper.ShopMapper;
import com.yike.user.param.BindingShopCameraParam;
import com.yike.user.service.ShopCameraService;
import com.yike.common.base.BaseServiceImpl;
import com.yike.user.utils.ZtskUtil;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 店铺摄像头表 服务实现类
 *
 * @author Sakura
 * @since 2024-09-12
 */
@Slf4j
@Service
public class ShopCameraServiceImpl extends BaseServiceImpl<ShopCameraMapper, ShopCamera> implements ShopCameraService {

    @Autowired
    private ShopCameraMapper shopCameraMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ZtskUtil ztskUtil;
    @Autowired
    private MachineMapper machineMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean binding(BindingShopCameraParam bindingShopCameraParam) throws Exception {
        // 获取店铺信息
        Shop shop = shopMapper.selectById(bindingShopCameraParam.getShopId());
        if (shop == null) {
            throw new BusinessException("店铺信息异常");
        }
        for (String cameraNo : bindingShopCameraParam.getCameraNos()) {
            // 绑定的时候需要验证设备是否属于当前代理商
            Machine machine = machineMapper.selectOne(Wrappers.lambdaQuery(Machine.class)
                    .eq(Machine::getMacNo, cameraNo)
                    .eq(Machine::getAgentId, LoginUtil.getAgentId()));
            if (machine == null) {
                throw new BusinessException("设备信息异常：" + cameraNo);
            }
            // 校验当前摄像头是否已绑定店铺
            ShopCamera shopCamera = shopCameraMapper.selectOne(Wrappers.lambdaQuery(ShopCamera.class)
                    .eq(ShopCamera::getCameraNo, cameraNo));
            if (shopCamera != null) {
                throw new BusinessException("摄像头已绑定店铺：" + cameraNo);
            }
            // 绑定
            shopCamera = new ShopCamera().setCameraNo(cameraNo).setShopId(shop.getId());

            super.save(shopCamera);
        }

        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean unbind(BindingShopCameraParam bindingShopCameraParam) throws Exception {
        // 不能全部删除必须保留一个
        // 获取当前所有设备数据
        Integer count = shopCameraMapper.selectCount(Wrappers.lambdaQuery(ShopCamera.class)
                .eq(ShopCamera::getShopId, bindingShopCameraParam.getShopId()));
        if (count != null && count.equals(bindingShopCameraParam.getCameraNos().size()) ) {
            throw new BusinessException("不能全部解除绑定，至少保留一个");
        }
        for (String cameraNo : bindingShopCameraParam.getCameraNos()) {
            // 直接删掉绑定信息即可
            super.remove(Wrappers.lambdaQuery(ShopCamera.class)
                    .eq(ShopCamera::getShopId, bindingShopCameraParam.getShopId())
                    .eq(ShopCamera::getCameraNo, cameraNo));
        }

        // 获取登录用户信息
        LoginUserInfoVo loginUserInfoVo = LoginUtil.getLoginUserInfoVo();

        // 同步解绑智通设备
        for (String devCode : bindingShopCameraParam.getDevCodes()) {
            ztskUtil.unbindDev(devCode, loginUserInfoVo.getZtskUserId(), loginUserInfoVo.getZtskToken());
        }

        return true;
    }

    @Override
    public List<String> getShopCameraList(String shopNo) throws Exception {
        // 获取店铺信息
        Shop shop = shopMapper.selectOne(Wrappers.lambdaQuery(Shop.class)
                .eq(Shop::getShopNo, shopNo));
        if (shop == null) {
            throw new BusinessException("店铺信息异常");
        }
        return shopCameraMapper.getShopCameraList(shop.getId());
    }

}
